#include "../common.h"
using namespace std;

char _GIT[MAX_BUILTIN_DESC_LENGTH];
char _obj[MAX_BUILTIN_DESC_LENGTH];
char _refs[MAX_BUILTIN_DESC_LENGTH];
char txt_index[MAX_BUILTIN_DESC_LENGTH];
char txt_HEAD[MAX_BUILTIN_DESC_LENGTH];
char refs_heads[MAX_BUILTIN_DESC_LENGTH];
REFS_heads _heads[MAX_REFS_HEADS_NUMS];

int heads_num=0;

extern BuiltinCmd blt[MAX_BUILTIN_FUNC_NUM];
extern char buf[80];
int git_init() {
    BuiltinCmd x;
    strcpy(x.name, "init");
    strcpy(x.desc, "Initialize the warehouse");
    x.hasAction = true;
    x.optNum = 2;
    strcpy(x.Help,"git init");
	strcpy(x.action, "Initialized empty Git repository in");
	addBuiltinCmd(&x);
	
//	strcpy(x.action[1],"Reinitialized existing Git repository in");
    return 0;
}

int git_init_cmdpline(int pos,int retc,char **retv){
	printf("%s %s\n",blt[pos].action,retv[3]);
	return 0;
}

int git_init_run(int retc,char **retv){
	FILE *fp=fopen("D:\\self-git\\git\\directory\\directory.txt","w");
	char dir[MAX_BUILTIN_DESC_LENGTH];
	chdir(retv[3]);
	getcwd(buf, sizeof(buf));
    printf("current working directory : %s\n", buf);
    fputc('\n',fp);
	fwrite(buf,1,strlen(buf),fp);
	fclose(fp);
	
	strcpy(_GIT,buf);
	strcat(_GIT,"\\.git");
	mkdir(_GIT);
	
	strcpy(_obj,_GIT);
	strcat(_obj,"\\objects");
	mkdir(_obj);
	
	strcpy(_refs,_GIT);
	strcat(_refs,"\\refs");
	mkdir(_refs);
	
	strcpy(txt_index,_GIT);
	strcat(txt_index,"\\index.txt");
	fp=fopen(txt_index,"w");	fclose(fp);
	
	strcpy(txt_HEAD,_GIT);
	strcat(txt_HEAD,"\\HEAD.txt");
	fp=fopen(txt_HEAD,"w");   fclose(fp);
	
	strcpy(refs_heads,_refs);
	strcat(refs_heads,"\\heads");
	mkdir(refs_heads);
	
	strcpy(_heads[heads_num++].name,refs_heads);
	strcat(_heads[heads_num-1].name,"\\master.txt");
    fp=fopen(_heads[heads_num-1].name,"w");  fclose(fp);
	 
	return 0;
}
